package xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.presenter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;

import com.sch.rfview.AnimRFRecyclerView;
import com.sch.rfview.manager.AnimRFLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.GetIndexTopData;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.IGetIndexTopData;

/**
 * Created by lenovo on 2016/10/19.
 */

public class IndexTopPresenter  {

    IndexRecyclerAdapter adapter;

    Context context;
    LayoutInflater inflator;
    GetIndexTopData indexTopData;
    private  int  currPage =1;
    //数据源准备
    List<Map<String,Object>> lists =new ArrayList<>();
    AnimRFRecyclerView recyclerview;
    //创建presenter的时候出来数据源
    public IndexTopPresenter(AnimRFRecyclerView _recyclerview, Context _context, LayoutInflater _inflator)
    {
        this.recyclerview =_recyclerview;
        this.context =_context;
        this.inflator =_inflator;
        this.indexTopData =new GetIndexTopData();

    }
    public void initRecyclerView()
    {
        indexTopData.getData(new IGetIndexTopData() {
            @Override
            public void postData(List<Object> _lists) {

                Log.i("TTTTTTT美购页面数据源的数量","---------->"+_lists.size()); //
                Log.i("---------------------->","-88888888888888->目前页码"+currPage);

                adapter =new IndexRecyclerAdapter(_lists, context,inflator,false);
                recyclerview.setLayoutManager(new AnimRFLinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
                recyclerview.setAdapter(adapter);

             }
        },currPage);


    }

    public void reload()
    {
        recyclerview.setLoadDataListener(new AnimRFRecyclerView.LoadDataListener() {
            @Override
            public void onRefresh() {
                if (currPage==1)
                {

                    indexTopData.getData(new IGetIndexTopData() {
                        @Override
                        public void postData(List<Object> _lists) {
                            adapter =new IndexRecyclerAdapter(_lists, context,inflator,false);
                            recyclerview.setLayoutManager(new AnimRFLinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
                            recyclerview.setAdapter(adapter);
                          // adapter.reloadListView(_lists,true);
                            recyclerview.refreshComplate();
                        }
                    },currPage);
                }
            }

            @Override
            public void onLoadMore() {
                currPage++;
                Log.i("---------------------->","--------------->目前页码"+currPage);
                indexTopData.getData(new IGetIndexTopData() {
                    @Override
                    public void postData(List<Object> _lists) {
                        Log.i("TAG","``````````````````"+_lists.size());
                        List<Object> list =new ArrayList<Object>();
//                        for (int i = 0; i < 4; i++) {
//                            list.add(_lists.get(i));
//                        }
                       if (_lists.size()>30)
                       {
                           for (int i = _lists.size()-30; i< _lists.size(); i++) {
                                 list.add(_lists.get(i));
                           }
                       }

                        adapter =new IndexRecyclerAdapter( list , context,inflator,true);
                        recyclerview.setLayoutManager(new AnimRFLinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
                        recyclerview.setAdapter(adapter);
                       // adapter.reloadListView(_lists,false);
                        recyclerview.loadMoreComplate();
                    }
                },currPage);

            }
        });
//       recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
//           @Override
//           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//               super.onScrolled(recyclerView, dx, dy);
//               if (dy<0)
//               {
////                   //小于0向上滑动
////                   currPage =1;
////                   indexTopData.getData(new IGetIndexTopData() {
////                       @Override
////                       public void postData(List<Object> _lists) {
////                           adapter =new IndexRecyclerAdapter(_lists, context,inflator,false);
////                           recyclerview.setLayoutManager(new AnimRFLinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
////                           recyclerview.setAdapter(adapter);
////
////
////                       }
////                   },currPage);
//               }
//           }
//       });
//      recyclerview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//          @Override
//          public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//             int length =scrollY -oldScrollY;
//              Log.i("scroY","---------->"+length);
//          }
//
//      });

    }

}
